<?php

namespace Console\Logic;

use \Common\Logic\BaseLogic;

/**
 * 设置控制器
 */
class SettingLogic extends BaseLogic {

    private $colorModel;
    private $eyeModel;
    private $sysinfoModel;

    public function __construct() {
        parent::__construct();
        $this->colorModel = D('ColorDic');
        $this->eyeModel = D('EyeDic');
        $this->sysinfoModel = D('Sysinfo');
    }

    /**
     * 获取颜色字典
     * @return type
     */
    public function getColorDic() {
        return $this->colorModel->select();
    }

    /**
     * 获取眼睛字典
     * @return type
     */
    public function getEyeDic() {
        return $this->eyeModel->select();
    }

    /**
     * 获取系统信息
     * @return type
     */
    public function getSysInfo() {
        return $this->sysinfoModel->find();
    }

    /**
     * 编辑系统设置
     * @param type $data
     * @return type
     */
    public function editSysInfo($data) {
        $id = $data["id"];
        unset($data["id"]);

        /**
         * 生成logo缩略图
         */
        if (!empty($data["logo"])) {
            //生成logo缩略图
            $url = $data['logo'];

            if (strrpos($url, '.')) {
                $imgType = substr($url, strrpos($url, '.') + 1);
            } else {
                $imgType = 'jpeg';
            }

            $miniImgUrl = "/pigeonSys/ImgFile/" . date("Ymd") . "_mini/" . time() . "_" . rand(1111, 9999) . '.' . $imgType;

            $miniFileName = WEB_PATH . "/.." . $miniImgUrl;
            $fileName = WEB_PATH . "/.." . $url;

            //等比例压缩，高度固定
            if (thumb($fileName, $miniFileName, array("h" => 130))) {
                $data['logo_mini'] = $miniImgUrl;
            }
        } else {
            $data['logo_mini'] = "";
        }

        if (empty($id)) {
            $sysInfo = $this->getSysInfo();
            $id = $sysInfo["id"];
        }

        if (empty($id)) {
            $res = $this->sysinfoModel->add($data);
        } else {
            $res = $this->sysinfoModel->where("id=$id")->save($data);
        }

        if (empty($res)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功");
    }

    /**
     * 添加颜色
     * @param type $userId
     * @param type $str
     * @return type
     */
    public function addColor($userId, $str) {
        if (empty($userId)) {
            return static::$res->err("添加人不能为空");
        }

        $str = trim($str);
        if (empty($str)) {
            return static::$res->err("颜色不能为空");
        }

        $where = array(
            "type" => $str,
        );

        if ($this->colorModel->where($where)->count()) {
            return static::$res->err("字段已存在");
        }

        $data = array(
            "type" => $str,
            "add_time" => date("Y-m-d H:i:s"),
            "add_user" => $userId
        );
        $id = $this->colorModel->add($data);
        if (empty($id)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功", $id);
    }

    /**
     * 添加眼睛
     * @param type $userId
     * @param type $str
     * @return type
     */
    public function addEye($userId, $str) {
        if (empty($userId)) {
            return static::$res->err("添加人不能为空");
        }

        $str = trim($str);
        if (empty($str)) {
            return static::$res->err("眼睛不能为空");
        }

        $where = array(
            "type" => $str,
        );

        if ($this->eyeModel->where($where)->count()) {
            return static::$res->err("字段已存在");
        }

        $data = array(
            "type" => $str,
            "add_time" => date("Y-m-d H:i:s"),
            "add_user" => $userId
        );
        $id = $this->eyeModel->add($data);
        if (empty($id)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功", $id);
    }

    /**
     * 编辑颜色
     * @param type $userId
     * @param type $id
     * @param type $str
     * @return type
     */
    public function editColor($userId, $id, $str) {
        if (empty($userId)) {
            return static::$res->err("编辑人不能为空");
        }

        if (empty($id)) {
            return static::$res->err("编辑ID不能为空");
        }

        $str = trim($str);
        if (empty($str)) {
            return static::$res->err("颜色不能为空");
        }

        $where = array(
            "type" => $str,
            "id" => array("neq", $id)
        );

        if ($this->colorModel->where($where)->count()) {
            return static::$res->err("字段已存在");
        }

        $data = array(
            "type" => $str,
            "edit_time" => date("Y-m-d H:i:s"),
            "edit_user" => $userId
        );
        $count = $this->colorModel->where("id=$id")->save($data);
        if (empty($count)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功");
    }

    /**
     * 编辑眼砂
     * @param type $userId
     * @param type $id
     * @param type $str
     * @return type
     */
    public function editEye($userId, $id, $str) {
        if (empty($userId)) {
            return static::$res->err("编辑人不能为空");
        }

        if (empty($id)) {
            return static::$res->err("编辑ID不能为空");
        }

        $str = trim($str);
        if (empty($str)) {
            return static::$res->err("眼砂不能为空");
        }

        $where = array(
            "type" => $str,
            "id" => array("neq", $id)
        );

        if ($this->eyeModel->where($where)->count()) {
            return static::$res->err("字段已存在");
        }

        $data = array(
            "type" => $str,
            "edit_time" => date("Y-m-d H:i:s"),
            "edit_user" => $userId
        );
        $count = $this->eyeModel->where("id=$id")->save($data);
        if (empty($count)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功");
    }

    /**
     * 删除颜色
     * @param type $id
     * @return type
     */
    public function delColor($id) {
        if (empty($id)) {
            return static::$res->err("编辑ID不能为空");
        }

        $count = $this->colorModel->delete($id);
        if (empty($count)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功");
    }

    /**
     * 删除眼砂
     * @param type $id
     * @return type
     */
    public function delEye($id) {
        if (empty($id)) {
            return static::$res->err("编辑ID不能为空");
        }

        $count = $this->eyeModel->delete($id);
        if (empty($count)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功");
    }

}
